<template>
  <div>
    <!--工具栏-->
    <div class="toolBar" style="float:left;padding-top: 10px;padding-left: 50px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="name">
          <el-input v-model="filters.name" placeholder="公司名称" style="width: 10em"></el-input>
        </el-form-item>
        <el-form-item prop="mainProducts">
          <el-input v-model="filters.mainProducts" placeholder="主营产品" style="width: 10em"></el-input>
        </el-form-item>
        <el-form-item prop="contact">
          <el-input v-model="filters.contact" placeholder="联系人" style="width: 10em"></el-input>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input v-model="filters.mobile" placeholder="联系电话" style="width: 10em"></el-input>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-search"
                     :label="$t('action.search')"
                     perms="supply:supplier:view"
                     @click="findPage"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-refresh"
                     :label="$t('action.reset')"
                     perms="supply:supplier:view"
                     @click="reset"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-plus"
                     :label="$t('action.add')"
                     perms="supply:supplier:add"
                     @click="showAdd"/>
        </el-form-item>
      </el-form>
    </div>

    <!--
        <div class="toolBar" style="float:right;padding-top: 10px;padding-left: 15px;">
          <el-form :inline="true" :size="size">
            <el-form-item>
              <el-button-group>
                <el-tooltip content="刷新" placement="top">
                  <el-button icon="fa fa-refresh" @click="findPage"></el-button>
                </el-tooltip>
                <el-tooltip content="列显示" placement="top">
                  <el-button icon="fa fa-filter" @click="displayFilterColumnDialog"></el-button>
                </el-tooltip>
                <el-tooltip content="导出" placement="top">
                  <el-button icon="fa fa-file-excel-o"></el-button>
                </el-tooltip>
              </el-button-group>
            </el-form-item>
          </el-form>
          &lt;!&ndash;表格显示列界面&ndash;&gt;
          <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns" @handleFilterColumns="handleFilterColumns">
          </table-column-filter-dialog>
        </div>
    -->

    <!--表格内容栏-->
    <!--不设置删除按钮 perms-delete="sale:goods:delete"-->
    <kt-table perms-edit="supply:supplier:edit" :data="pageResult" :columns="filterColumns" @findPage="findPage"
              @handleEdit="showEdit"></kt-table>

    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="30%" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="100px" :rules="dataFormRules" ref="dataForm" :size="size"
               label-position="right">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="false"></el-input>
        </el-form-item>
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="dataForm.name" maxlength="50" auto-complete="false"></el-input>
        </el-form-item>
        <el-form-item label="主营产品" prop="innerCode">
          <el-input v-model="dataForm.mainProducts" auto-complete="false"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="dataForm.contact" auto-complete="false"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="dataForm.mobile" maxlength="11" :show-word-limit="true" auto-complete="false"></el-input>
        </el-form-item>
        <el-form-item label="链接地址" prop="introUrl">
          <el-input v-model="dataForm.introUrl" auto-complete="false"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" autosize :show-word-limit="true" v-model="dataForm.remark" maxlength="100">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button :size="size" @click.native="dialogVisible=false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size" type="primary" :loading="editLoading" @click.native="submitForm">
          {{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import KtTable from '../core/KtTable'
  import KtButton from '../core/KtButton'
  import TableColumnFilterDialog from '../core/TableColumnFilterDialog'

  export default {
    name: "Supplier",
    components: {
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: 'small',
        filters: {
          name: '',
          contact: '',
          mobile: '',
          mainProducts: ''
        },
        columns: [],//初始表格列显示字段
        filterColumns: [],//列显示过滤字段
        pageRequest: {},
        pageResult: {},
        operation: false,//true新增 false编辑
        dialogVisible: false,//新增编辑界面是否显示
        editLoading: false,
        //新增或编辑页面
        dataForm: {
          id: 0,
          name: '',
          mainProducts: '',
          contact: '',
          mobile: '',
          introUrl: '',
          remark: ''
        },
        dataFormRules: {
          name: [
            {required: true, message: '请输入供应商名称', trigger: 'blur'}
          ],
          mainProducts: [
            {required: true, message: '请介绍主营产品', trigger: 'blur'}
          ],
          contact: [
            {required: true, message: '请填写联系人', trigger: 'blur'}
          ],
          mobile: [
            {required: true, message: '请填写联系电话', trigger: 'blur'}
          ]
        },
      }
    },
    methods: {
      // 处理表格列过滤显示
      initColumns() {
        this.columns = [
          {prop: "id", label: "编号", minWidth: 50},
          {prop: "name", label: "公司名称", showOverflow: true, minWidth: 120},
          {prop: "mainProducts", label: "主营产品", showOverflow: true, minWidth: 120},
          {prop: "contact", label: "联系人", minWidth: 120},
          {prop: "mobile", label: "联系电话", minWidth: 120},
          {prop: "introUrl", label: "链接地址", showOverflow: true, hrefUrl: true, minWidth: 120},
          {prop: "remark", label: "备注", showOverflow: true, minWidth: 120},
        //  {prop: "lastUpdator", label: "最近操作者", minWidth: 130},
        //  {prop: "lastUpdateTime", label: "最近操作时间", minWidth: 140}
        ];
        this.filterColumns = JSON.parse(JSON.stringify(this.columns));
      },
      //处理表格列过滤显示
      displayFilterColumnDialog() {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns(data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      //获取分页数据
      findPage(data) {
        if ((data !== null && typeof data !== 'undefined') && typeof data.pageRequest !== 'undefined') {
          this.pageRequest = data.pageRequest;
        }
        this.pageRequest.params = {
          name: this.filters.name,
          mobile: this.filters.mobile,
          contact: this.filters.contact,
          mainProducts: this.filters.mainProducts
        };
        this.$api.supplier.findPage(this.pageRequest).then((res) => {
          if (res.code === 200) {
            this.pageResult = res.data;
          }
        }).then((data !== null && typeof data !== 'undefined') ? data.callback : '');
      },
      //显示新增界面
      showAdd() {
        this.dialogVisible = true;
        this.operation = true;
      },
      //显示编辑界面
      showEdit(params) {
        this.dialogVisible = true;
        this.operation = false;
        this.dataForm = Object.assign({}, params.row);
      },
      //编辑
      submitForm() {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗?', '提示', {}).then(() => {
              this.editLoading = true;
              let params = Object.assign({}, this.dataForm);
              this.$api.supplier.save(params).then((res) => {
                this.editLoading = false;
                if (res.code === 200) {
                  this.$message({message: '操作成功', type: 'success'});
                  this.dialogVisible = false;
                  this.$refs['dataForm'].resetFields();
                } else {
                  this.$message({message: res.message, type: 'error'});
                }
                this.findPage();
              })
            });
          }
        })
      },
      reset() {
        this.$refs['filters'].resetFields();
        this.findPage();
      }
    },
    mounted() {
      this.initColumns();
    }
  }
</script>

<style scoped>

</style>
